package com.witframework.web.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class KeepSessionTag extends BodyTagSupport{
  private int timeout = 120;
  private boolean cond = true;
  private String sBody = null;

  public void setTimeout(int paramInt)  {
    this.timeout = paramInt;
  }

  public int getTimeout()  {
    return this.timeout;
  }

  public void setCond(boolean paramBoolean)  {
    this.cond = paramBoolean;
  }

  public boolean getCond()  {
    return this.cond;
  }

  public int doStartTag()    throws JspException  {
    if (!(this.cond))
      return 0;
    return 2;
  }

  public int doAfterBody()    throws JspException  {
    BodyContent localBodyContent = getBodyContent();
    if (localBodyContent != null)    {
      this.sBody = localBodyContent.getString();
      localBodyContent.clearBody();
    }else    {
      this.sBody = "";
    }
    return 0;
  }

  public int doEndTag()    throws JspException  {
    if (!(this.cond))    {
      dropData();
      return 6;
    }
    StringBuffer localStringBuffer = new StringBuffer("");
    localStringBuffer.append(getJavaScript());
    localStringBuffer.append("<img id='keepsession' width='0' height='0' src='keepsession.gif?' alt=''>");
    JspWriter localJspWriter = this.pageContext.getOut();
    try    {
      localJspWriter.print(localStringBuffer.toString());
    }    catch (IOException localIOException)
    {
      throw new JspException("KeepSession tag: could not save data");
    }
    dropData();
    return 6;
  }

  public void release()  {
    dropData();
  }

  private void dropData()  {
    this.timeout = 120;
    this.cond = true;
  }

  private String getJavaScript()  {
    String str = "\n<script language=\"JavaScript\" type=\"text/javascript\">\nfunction keepSession() {var img=document.getElementById('keepsession');if (img) img.src=img.src.replace(/\\?.*$/, '?' + new Date().getTime());} window.setInterval('keepSession()'," + (this.timeout * 1000) + ");\n" + "</script>\n";
    return str;
  }
}